package com.qf.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qf.consants.RabbitMQConstants;
import com.qf.entity.Goods;
import com.qf.resp.ResultEntity;
import com.qf.service.IGoodsService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/goodsController")
public class GoodsController {

    @Autowired
    private IGoodsService goodsService;


    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/getGoodsPage")
    public Page<Goods> getGoodsPage(@RequestBody Page<Goods> page){
        return goodsService.selectPage(page);
    }

    @RequestMapping("/addGoods")
    public ResultEntity addGoods(@RequestBody Goods goods){
        boolean insert = goodsService.insert(goods);
        if(insert){
            // 发送消息给MQ
            rabbitTemplate.convertAndSend(RabbitMQConstants.GOODS_EXCAHNGE,"",goods);
        }
        return ResultEntity.success(insert);
    }

    @RequestMapping("/getGoodsById/{id}")
    public Goods getGoodsById(@PathVariable("id") Integer id){
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.eq("id",id);
        return goodsService.selectOne(entityWrapper);
    }
}
